<?php
defined('BASEPATH') OR exit('No direct script access allowed');

class Server extends Backend {

	public function __construct()
	{
		parent::__construct();
		$this->lang->load('sites');
		$this->load->model(['server_model','site_model']);
		$this->data['page_home']  = lang('lang_dashboard');
		$this->data['page_title'] = lang('lang_server');
				
	}


	public function index()
	{

		
		// Title
		$this->data['meta_title'] = $this->data['page_title'];

		// Breadcrumbs
		$this->data['array_breadcrumbs'] = array(
			$this->data['page_home']  => 'backend',
			$this->data['page_title'] => 'backend/server'
		);

		if(IS_POST && $this->input->post("action"))
        {
            if($this->input->post("action")=="add")
            {
	

                $newdata=[
                    "ipaddr"=>$this->input->post("ipaddr"),
                    "username"=>$this->input->post("username"),
                    "pass"=>$this->input->post("pass"),
					"bturl"=>$this->input->post("bturl"),					
					"memo"=>$this->input->post("memo"),
					"rootpass"=>$this->input->post("rootpass"),
				];

                $this->server_model->create($newdata);
                redirect('backend/server', 'refresh');
                die();
            }
		}

		$page=max(1,(int) $this->input->get('page'));
		$key=trim($this->input->get('keyword'));
		$page_data=$this->server_model->getpage($page,$key);
	
        $this->data['data']=$page_data["data"];
        $this->data['pager']=$page_data["pager"];
		$this->data['total']=$page_data["total"];
        $this->data['pages']=$page_data["pages"];
		$this->data['page']=$page;    
		
		$keyword = array(
				'name'      => 'keyword',
				'id'        => 'keyword',
				'value'     =>  $key,
				'class'     =>  'form-control input-sm',
				'placeholder'=> '请输入关键字',
				'maxlength' => '100'
		);

		$this->data['keyword']=$keyword;
		$this->data['content_header_title'] = content_header_title($this->data['page_title']);
		$this->data['content'] = 'server/index';

		$config = $this->systemconfig_model->get_configs(); 
		$this->data['config']=$config;

		$this->render();
	}
	
	public function vultr()
	{
		$config = $this->systemconfig_model->get_configs();

		$ch = curl_init("https://api.vultr.com/v1/account/info");
        curl_setopt($ch, CURLOPT_RETURNTRANSFER,true);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
        curl_setopt($ch, CURLOPT_HEADER, 0);
        curl_setopt($ch, CURLOPT_HTTPHEADER, array(
            'API-Key: '.$config["vultr"],
        ));
        $result = curl_exec($ch);
        if (curl_errno($ch)) {
            //print curl_error($ch);
        }
		curl_close($ch);

		$info=json_decode($result,true);
		if(is_array($info))
		{
			$msg="Vultr账户余额 $".(0-$info["balance"]-$info["pending_charges"])." , 本月已消费 $".$info["pending_charges"];
		}
		else
		{
			$msg=$result;
		}
		echo $msg;
		die();
	}

	public function sites($serverid)
	{
		$server=$this->server_model->getitembyid($serverid);
		if(!$server) die('error link');

		if(IS_POST && $this->input->post("action"))
        {
            if($this->input->post("action")=="add")
            {
	
				$siteid=$this->input->post("siteid");
                $this->site_model->update($siteid,["serverid"=>$serverid]);
                redirect('backend/server/sites/'.$serverid, 'refresh');
                die();
            }
		}
		$listsites=$this->site_model->byserver($serverid);
		
		$sites=$this->site_model->getallsync();
		$this->data["sites"]=$sites;
		$this->data["listsites"]=$listsites;
		$this->data['content_header_title'] = content_header_title($server["ipaddr"]." 网站列表");
		$this->data['content'] = 'server/sites';

		$this->render();
	}

	public function modify($siteid)
	{
		$site=$this->server_model->getitembyid($siteid);
		if(!$site) die('error link');
		
	
		$this->data["site"]=$site;
		$this->data['content'] = 'server/modify';    
		$this->clean_render();
	}

    public function ajaxedit()
    {
        $attrName=$this->input->post("attrName");
		$newHtml=$this->input->post("newHtml");
		if(!in_array($attrName,['ipaddr','username','pass','rootpass','bturl'])) die();
        $cid=$this->input->post("cid");
        $newdata[$attrName]=$newHtml;
        $this->server_model->update($cid, $newdata);
        echo json_encode([]);
        die();
	}

	public function delsite()
    {

        $return=["status"=>1];
		$cid=$this->input->post("cid");
        if(!$this->site_model->update($cid,["serverid"=>0]))
        {
            $return["status"]=0;
            $return["msg"]="删除失败";
        }       
        echo json_encode($return);
        die();
	}
	
	
}
